import 'dart:convert';
import 'dart:developer';

import 'package:ball_lion/utils/user.dart';
import 'package:ball_lion/utils/utils.dart';

class ResourceConfigEntity {
  int? infoClassId;
  bool? hotMatch;
  bool? hotExpert;
  int? sportsId;
  int? leagueId;
  String? leagueName;

  ResourceConfigEntity(
      {this.infoClassId,
      this.hotMatch,
      this.hotExpert,
      this.sportsId,
      this.leagueId,
      this.leagueName});

  ResourceConfigEntity.fromContent(String? content) {
    Map<String, dynamic> json = jsonDecode(content ?? "");
    infoClassId = json['infoClassId'];
    hotMatch = json['hotMatch'];
    hotExpert = json['hotExpert'];
    sportsId = json['sportsId'] is int
        ? json['sportsId']
        : int.tryParse(json['sportsId'] ?? "");
    leagueId = json['leagueId'] is int
        ? json['leagueId']
        : int.tryParse(json['leagueId'] ?? "");
    leagueName = json['leagueName'];
  }

  ResourceConfigEntity.fromJson(Map<String, dynamic> json) {
    infoClassId = json['infoClassId'];
    hotMatch = json['hotMatch'];
    hotExpert = json['hotExpert'];
    sportsId = json['sportsId'];
    leagueId = json['leagueId'];
    leagueName = json['leagueName'];
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['infoClassId'] = this.infoClassId;
    data['hotMatch'] = this.hotMatch;
    data['hotExpert'] = this.hotExpert;
    data['sportsId'] = this.sportsId;
    data['leagueId'] = this.leagueId;
    data['leagueName'] = this.leagueName;
    return data;
  }
}

class ResourceDialogConfigEntity {
  int? duration;
  String? route;
  String? content;
  double? btnTop;
  double? btnHeight;
  //版权视频比赛
  double? cancelW;
  double? cancelH;
  double? cancelX;
  double? cancelY;
  double? imgW;
  double? imgH;
  ResourceDialogConfigEntity(
      {this.duration,
      this.route,
      this.content,
      this.btnTop,
      this.btnHeight,
      this.cancelW,
      this.cancelH,
      this.cancelX,
      this.cancelY,
      this.imgW,
      this.imgH});

  ResourceDialogConfigEntity.fromJson(Map<String, dynamic> json) {
    duration = int.tryParse(json['duration'] ?? "");
    route = json['route'];
    content = json['content'];
    btnTop = json['btnTop'];
    btnHeight = json['btnHeight'];
    cancelW = double.tryParse(json['cancelW'].toString());
    cancelH = double.tryParse(json['cancelH'].toString());
    cancelX = double.tryParse(json['cancelX'].toString());
    cancelY = double.tryParse(json['cancelY'].toString());
    imgW = double.tryParse(json['imgW'].toString());
    imgH = double.tryParse(json['imgH'].toString());
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['duration'] = this.duration;
    data['route'] = this.route;
    data['content'] = this.content;
    data['btnTop'] = this.btnTop;
    data['btnHeight'] = this.btnHeight;
    data['cancelW'] = this.cancelW;
    data['cancelH'] = this.cancelH;
    data['cancelX'] = this.cancelX;
    data['cancelY'] = this.cancelY;
    data['imgW'] = this.imgW;
    data['imgH'] = this.imgH;
    return data;
  }
}

class ResourceEntity {
  int? id;
  String? classId;
  String? title;
  String? content;
  String? href;
  String? imgUrl;
  int? needLogin;
  String? button;
  String? extInfo;
  int? sort;

  late ResourceConfigEntity? configFromContent = () {
    try {
      final json = jsonDecode(content ?? "");
      return ResourceConfigEntity.fromJson(json);
    } catch (err) {}
  }.call();

  late ResourceDialogConfigEntity? dialogConfig = () {
    try {
      final json = jsonDecode(content ?? "");
      final entity = ResourceDialogConfigEntity.fromJson(json);
      return entity;
    } catch (err) {
      log('-----ResourceDialogConfigEntity $err');
    }
  }.call();

  ResourceEntity(
      {this.id,
      this.classId,
      this.title,
      this.content,
      this.href,
      this.imgUrl,
      this.needLogin,
      this.button,
      this.extInfo,
      this.sort});

  ResourceEntity.fromJson(Map<String, dynamic> json) {
    id = json['id'];
    classId = json['classId'];
    title = json['title'];
    content = json['content'];
    href = json['href'];
    imgUrl = json['imgUrl'];
    needLogin = json['needLogin'];
    button = json['button'];
    extInfo = json['extInfo'];
    sort = json['sort'];
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['id'] = this.id;
    data['classId'] = this.classId;
    data['title'] = this.title;
    data['content'] = this.content;
    data['href'] = this.href;
    data['imgUrl'] = this.imgUrl;
    data['needLogin'] = this.needLogin;
    data['button'] = this.button;
    data['extInfo'] = this.extInfo;
    data['sort'] = this.sort;
    return data;
  }
}

extension ResourceEntityEx1 on ResourceEntity {
  doJump() async {
    if (href?.isEmpty ?? true) {
      return;
    }
    final ref = href!;

    if ((needLogin ?? 0) > 0) {
      await User.needLogin(() async {});
      if (!User.isLogin) {
        return;
      }
    }

    Utils.doJump(ref);
  }
}
